package br.edu.utfpr.backuptools.BackupTools.app;

import java.rmi.RemoteException;

import org.quartz.SchedulerException;

import br.edu.utfpr.backuptools.BackupTools.model.pojo.Configuracao;
import br.edu.utfpr.backuptools.BackupTools.rmi.RmiCliente;
import br.edu.utfpr.backuptools.BackupTools.util.quartz.QuartzConfigure;
import br.edu.utfpr.backuptools.BackupTools.view.action.AbstractConsultaAction;
import br.edu.utfpr.backuptools.BackupTools.view.action.configuracao.ListarConfiguracaoAction;
import br.edu.utfpr.backuptools.BackupTools.view.telas.principal.JanelaPrincipal;

/**
 * Classe main da aplicação
 * 
 * Chama a janela principal e configura o Quartz com o horario da configuração ativa.
 * 
 * @author Luan Malaguti Reffatti
 *
 */
public class Aplication {
	static QuartzConfigure quartzConfigure;
	
	public static void main(String[] args) {
		try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(JanelaPrincipal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(JanelaPrincipal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(JanelaPrincipal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(JanelaPrincipal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
       
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new JanelaPrincipal().setVisible(true);
                try {
					try {
						RmiCliente provider = new RmiCliente();
						provider.play();
					} catch (RemoteException e) {
						e.printStackTrace();
					}
					
                	agendaBackups();
				} catch (SchedulerException e) {
					e.printStackTrace();
				}
            }
        });
	}
	
	public static void agendaBackups() throws SchedulerException
	{
		AbstractConsultaAction<Configuracao> actionCosultaConf = new ListarConfiguracaoAction();
		quartzConfigure = new QuartzConfigure();
		
		Configuracao con = actionCosultaConf.execute().get(0);
		quartzConfigure.configure(con.getHora(), con.getMinutos());
	}
	
	public static void agendaBackups(Configuracao conf){
		quartzConfigure.reConfigure(conf.getHora(), conf.getMinutos());
	}
}
